package com.fz.song.controller.admin;

import com.fz.song.config.BlogConfig;
import com.fz.song.pojo.BlogAdmin;
import com.fz.song.service.IBlogAdminService;
import com.fz.song.service.impl.BlogAdminServiceImpl;
import com.fz.song.util.ResultObj;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by sfz on 2018/5/23.
 * 主页controller
 */
@Controller
@RequestMapping("/admin")
public class AdminController {
    private static Log logger = LogFactory.getLog(AdminController.class);
    @Autowired
    private IBlogAdminService blogAdminService;

    @RequestMapping("/")
    public String index_one() {

        return "redirect:/admin/index.html";
    }

    @RequestMapping("/index.html")
    public String indexhtml() {

        return "admin/index";
    }

    @RequestMapping("/login.html")
    public String loginhtml() {
        return "admin/login";
    }

    @RequestMapping("/login")
    @ResponseBody
    public Object login(HttpServletRequest request,BlogAdmin blogAdmin) {
        ResultObj login = blogAdminService.login(request, blogAdmin);
        logger.info("登录结果:"+login.isStatus());
        return login;
    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.removeAttribute(BlogConfig.ADMIN_LOGIN);
        logger.info("退出系统");
        return "redirect:/admin/index.html";
    }
}
